package com.example.mainstream.ui.user;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.shopapp.App;
import com.example.shopapp.R;
import com.example.shopapp.adapter.GoodsAdapter;
import com.example.shopapp.adapter.IGoodsListener;
import com.example.shopapp.bean.Goods;
import com.example.shopapp.bean.Select;
import com.example.shopapp.dao.SelectDao;
import com.example.shopapp.database.GoodsDBUtils;
import com.example.shopapp.database.GreenDaoManager;
import com.example.shopapp.databinding.ActivityLiulanListBinding;
import com.example.shopapp.widget.MyItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.project.base.baseui.BaseNormalActivity;

public class RecordActivity extends BaseNormalActivity<ActivityLiulanListBinding> implements IGoodsListener {

    GoodsAdapter goodsAdapter;
    SelectDao selectDao;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_liulan_list;
    }

    @Override
    protected void initView() {
        selectDao = GreenDaoManager.getmInstance().getDaoSession().getSelectDao();
        mBinding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.toolbar.tvTitle.setText("浏览记录");
    }

    @Override
    protected void initData() {
        goodsAdapter = new GoodsAdapter(this,RecordActivity.this);
        mBinding.rlAllShop.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
        mBinding.rlAllShop.addItemDecoration(new MyItemDecoration(10));
        mBinding.rlAllShop.setAdapter(goodsAdapter);
        getGoodList();
    }
    private void getGoodList(){
        List<Select> selectList = selectDao.loadAll();
        if (selectList ==null || selectList.size() ==0){
            goodsAdapter.setNewData(null);
            goodsAdapter.notifyDataSetChanged();
            return;
        }
        List<Goods> goodsList = GoodsDBUtils.getInstance(getApplicationContext()).getGoodsList();
        if (goodsList.size() ==0){
            goodsAdapter.setNewData(null);
            goodsAdapter.notifyDataSetChanged();
            return;
        }
        List<Goods> list = new ArrayList<>();
        for (int i = 0; i < selectList.size(); i++) {
            for (int j = 0; j < goodsList.size(); j++) {
                if (selectList.get(i).getGood_id() == goodsList.get(j).getId() && App.getInstance().user.getUser_id().equals(selectList.get(i).getUser_id())){

                    list.add(goodsList.get(j));
                }
            }
        }
        Collections.reverse(list);
        goodsAdapter.setNewData(list);
        goodsAdapter.notifyDataSetChanged();

    }
    @Override
    public void onGoodsClick(Goods goods) {

    }
}
